import {
  Controller,
  Inject,
  Post,
  Get,
  Put,
  Del,
  Body,
  Query,
  Param
} from "@midwayjs/core";
import { Utils } from "../utils";
import { PrizeActivityService } from "../service/prize.activity.service";
import {
  PrizeActivityCreateDto,
  PrizeActivityPageQueryDto,
  PrizeActivityUpdateDto
} from "../dto/prize.activity.dto";
import { ApiTags, ApiOperation } from "@midwayjs/swagger";

@ApiTags(["抽奖活动管理"])
@Controller("/auth-api/prize/activity")
export class PrizeActivityController {
  @Inject()
  prizeActivityService: PrizeActivityService;

  @Inject()
  utils: Utils;

  @ApiOperation({ summary: "创建抽奖活动批次" })
  @Post("/")
  async create(@Body() dto: PrizeActivityCreateDto) {
    const result = await this.prizeActivityService.create(dto);
    return this.utils.response.success(result);
  }

  @ApiOperation({ summary: "创建活动批次并生成抽奖码" })
  @Post("/create-with-results")
  async createActivityAndResult(
    @Body()
    dto: PrizeActivityCreateDto
  ) {
    const result = await this.prizeActivityService.createActivityAndResult(dto);
    return this.utils.response.success(result);
  }

  @ApiOperation({ summary: "获取分页活动列表" })
  @Get("/page")
  async page(@Query() query: PrizeActivityPageQueryDto) {
    const result = await this.prizeActivityService.page(query);
    return this.utils.response.success(result);
  }

  @ApiOperation({ summary: "获取全部活动列表" })
  @Get("/list")
  async list(
    @Query() query: { batchName?: string; status?: number; groupId?: string }
  ) {
    const result = await this.prizeActivityService.list(query);
    return this.utils.response({ ...result, data: result.data.list });
  }

  @ApiOperation({ summary: "更新活动状态" })
  @Put("/:id/status")
  async updateStatus(@Param("id") id: string, @Body("status") status: number) {
    const result = await this.prizeActivityService.updateStatus(id, status);
    return this.utils.response.success(result);
  }

  @ApiOperation({ summary: "更新活动批次" })
  @Put("/:id")
  async update(@Param("id") id: string, @Body() dto: PrizeActivityUpdateDto) {
    dto.id = id; // 确保ID正确
    const result = await this.prizeActivityService.update(dto);
    return this.utils.response.success(result);
  }

  @ApiOperation({ summary: "删除活动批次" })
  @Del("/:id")
  async remove(@Param("id") id: string) {
    const result = await this.prizeActivityService.remove(id);
    return this.utils.response(result);
  }

  @ApiOperation({ summary: "获取活动详情" })
  @Get("/:id")
  async info(@Param("id") id: string) {
    const result = await this.prizeActivityService.info(id);
    return this.utils.response.success(result);
  }
}
